package com.redtree.air.sqlite
{
	import flash.data.*;
	import flash.data.SQLTransactionLockType;
	import flash.errors.SQLError;
	import flash.events.*;

	[Event(name="result", type="flash.events.SQLEvent")]
	[Event(name="error", type="flash.events.SQLErrorEvent")]
	public class Query extends EventDispatcher
	{
		
		private var _connection:SQLConnection;
		private var _statement:SQLStatement;
		private var _sql:String;
		private var _parameters:Array;
		private var _data:Array;
	
		[Bindable]
		public function get connection():SQLConnection
		{
			return _connection;
		}
		
		public function set connection(conn:SQLConnection):void
		{
			_connection = conn;
		}
		
		[Bindable]
		public function get sql():String
		{
			return _sql;
		}
		
		public function set sql(value:String):void
		{
			_sql = value;
		}
		
		[Bindable]
		public function get parameters():Array
		{
			return _parameters;
		}
		
		public function set parameters(params:Array):void
		{
			_parameters = params;
		}		
		
		[Bindable]
		public function get data():Array
		{
			return _data;
		}
		
		public function set data(result:Array):void
		{
			_data = result;
		}				
		
		public function execute(ownTransaction:Boolean=true):Boolean
		{
			_statement = new SQLStatement();
			_statement.sqlConnection = _connection;
			_statement.text = _sql;
			if(_parameters) {
				for(var i:uint=0; i<_parameters.length; i++)
				{
					_statement.parameters[i] = _parameters[i];
				}
			}
			if( ownTransaction ) _connection.begin(SQLTransactionLockType.IMMEDIATE);
			try{
				_statement.execute();
				var result:SQLResult = _statement.getResult();
				_data = result.data is Array ? result.data : [{rows: result.rowsAffected}];
				if( ownTransaction ) _connection.commit();
			}catch( error:SQLError ){
				if( ownTransaction )_connection.rollback();
				return false;
			}
			return true;			
		}		
	}
}